/* 
* @Author: LDF QQ 47121862
* @Date:   2014-05-16 17:34:12
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-06-11 09:20:15
*/
//================================================================================
//扩展开始
$.fn.extend({
	pos:function(){ //居中
		var t = ($(window).height() - $(this).height())/2 + $(window).scrollTop();
			 l = ($(window).width() - $(this).width())/2;
		$(this).css({'top':t,'left':l});
		return this;
	},
	drag:function(limit){ //拖拽,默认不限制范围
		var id = $(this);
		id.css('cursor','move');
		id.mousedown(function(e){
			var ls = e.pageX - $(this).position().left,
			 	 ts = e.pageY - $(this).position().top,
			 	 w = $(window).width() - $(this).width(),
			 	 h = $(window).height() - $(this).height() + $(window).scrollTop(),
			 	 _this=$(this);	
			$(document).on('mousemove',function(e){
				var l = e.pageX -ls,
			 	 	 t = e.pageY - ts;	
				_this.css({'left':l,'top':t});
				if(limit){
					if(_this.position().left<=0)
						_this.css('left',0);
					if(_this.position().top<=0)
						_this.css('top',0);
					if(_this.position().left>=w)
						_this.css('left',w);
					if(_this.position().top>=h)
						_this.css('top',h);
				}
			})
			return false;
		})
		$(document).on('mouseup',function(){
			$(document).off('mousemove');
		})
		return this;
	},
	showPic:function(option){//自动显示大图
		var timer=null,id=$(this),
			opt={
				fllow:0
			};
		$.extend(opt,option);
		$(this).hover(function(e){
			var pic = $(this).children('img').attr('src'),
				 h = '<img class="showPicCont" src='+pic+' />',
				 p = $(this).children('img');
			if($('.showPicCont').length==0)
				$('body').append(h);
			if(opt.fllow){ //是否开启鼠标跟随
				$(document).mousemove(function(e){
					id.showPicFn(e);
			 	}); 
			}else{
				timer=setTimeout(function(){
					id.showPicFn(e);
				},500)
			}
		},function(){
			$('.showPicCont').remove();
			clearTimeout(timer);
		})
		id.showPicFn=function(e){ //图片出现
			var sp = $('.showPicCont'),
		 	 l = e.pageX+25,
		 	 t = e.pageY+25,
		 	 lmax = $(window).outerWidth()-sp.outerWidth(),
		 	 tmax = $(window).outerHeight()-sp.outerHeight();
		 	 sp.fadeIn().css({'left':l,'top':t});
		 	 if(l>lmax) sp.css('left',lmax);
		 	 if(l<=0) sp.css('left',l);
		 	 if(t<=0) sp.css('top',t);
		 	 if(t>tmax) sp.css('top',tmax);
		}
	},
	chSelect:function(opt){ //模拟下拉框
		var _name=$(this).attr('name'),_this=$(this),
			sel='<div class="chSelect selected">',op='<div class="subSelect">',
			opFirst=$(this).find('option').first().text(),
			inp='<input type="hidden" name="'+_name+'" />',actValue=null,actName=null;
		this.bornOption=function(){ //生成option
			_this.find('option').each(function(){
				var val = $(this).val(),opName = $(this).text(),opDisabled=$(this).attr('disabled'),
					opSelected=$(this).attr('selected');
					opSelected = opSelected?'selected="selected"':'';
				if(opDisabled) { //增加disabled属性
					opDisabled = 'disabled';
					op+='<p class="sub '+opDisabled+'" value="'+val+'" disabled='+opDisabled+' '+opSelected+'>'+opName+'</p>';
				}else{
					op+='<p class="sub" value="'+val+'" '+opSelected+'>'+opName+'</p>';
				}
			})
		}
		this.bornSelect=function(){ //生成Select
			this.bornOption()	;
			sel+='<b class="selVal">'+opFirst+'</b><i class="Down"></i>'+op+'</div></div>';
			_this.parent().append(sel).append(inp).end().remove();
		}

		this.bornSelect();

		// var select = $('.chSelect'),subSel=select.children('div'),ops=subSel.children('p'),_input=$('input[name="'+_name+'"]'),selVal = select.children('.selVal');
		this.addEv=function(){ //增加点击事件
			var select = $('.chSelect'),_input=$('input[name="'+_name+'"]');
			select.each(function(i){
				var _stat=0;
				$(this).click(function(){
					var _this=$(this),subSel=_this.children('div'),ops=subSel.children('p'),selVal = _this.children('.selVal'),stat=0;
					if(opt){
						if(subSel.height()>=300 && _stat==0){
							var _width=(_this.outerWidth()-2)*4;
							ops.css({'display':'inline-block','border':'1px solid #eee','margin':'2px'});
							_stat =1;
						}else{
							if(_stat==0) var _width=(_this.outerWidth()-2);
						}
					}else{
						var _width=(_this.outerWidth()-2);
					}
					subSel.css({'top':_this.outerHeight()-2,'left':'-1px','width':_width});
					subSel.slideDown();
					ops.click(function(){ //下拉框点击事件
						var _val = $(this).attr('value'),_name=$(this).text();
						if($(this).attr('disabled')) return false;
						_input.val(_val);
						selVal.text(_name);
						subSel.slideUp(100);
						return false;
					})
					return false;
				})
				//默认选中selected
				var ops = $(this).children('div').children('p'),selVal = $(this).children('.selVal');
				ops.each(function(){
					if($(this).attr('selected')) {
						actValue = $(this).attr('value');
						actName = $(this).text();
					}
				})
				selVal.text(actName);
				_input.val(actValue);

			})
			$(document).click(function(){
				select.children('div').slideUp(100);
			})
		}
		this.addEv();
	}



})
$.extend({
	//弹窗消息
	msg:function(options){
		var _this=this,
			 _def={
			 	str:'欢迎使用：LDF简单弹出消息窗口！', //默认内容
			 	timeout:2, //关闭时间
			 	shade:0, //是否有遮罩
			 	closebtn:0, //是否显示关闭按钮
			 	css:{width:null,height:null},
			 	close:function(){} //回调函数
			 },
			 opt = $.extend(_def,options),
			 timer=null;
			 _this.ind = 0; //id标记
		this.close=function(){
			if(m.length<=1) _this.ind=0;
			m[_this.ind].remove();
			bg[_this.ind].remove();
			_this.ind++;
			return this;
		}
		var d = $.getSize($(document)),div='',bg='';
		bg+='<div class="msgContentClassBg"></div>'
		div += '<div class="msgContentClass">\
			<b class="closeMsgB">×</b>\
			'+opt.str+'</div>';
		if($('div.msgContentClass').length<=2)
			$('html').append(bg).append(div);
		var bg = $('.msgContentClassBg'),m=$('.msgContentClass'),c=$('.closeMsgB');
		if(opt.shade) bg.width((d.w)).height((d.h)).css({'opacity':.3,'top':0,'left':0}).fadeIn();
		if(!opt.closebtn) c.remove();
		if(opt.css.width) m.css({'width':opt.css.width+'px','height':opt.css.height+'px'});
		m.fadeIn().pos();
		timer=setTimeout(function(){
					_this.close();
					opt.close();
				},opt.timeout*500)
		$(document).on({'keydown':function(e){
			if(e.keyCode==27) {
				clearTimeout(timer);
				_this.close();
				opt.close();
			}
		}})
		c.click(function(){
			clearTimeout(timer);
			_this.close();
			opt.close();
		})
		return this;
	},
	getSize:function(id){ //获得尺寸
		var s = {};
		s.w = id.width();
		s.h = id.height();
		s.l = id.scrollLeft();
		s.t = id.scrollTop();
		return s;
	},
	confirmd:function(options){ //确认框
		var _def={ yes:null, no:null, msg:'您确定要继续吗?' },
			 opt = $.extend(_def, options);
		$.addConfirmd(opt.msg);
		var c = $('.LDFconfirm'),
			 bg = $('.msgContentClassBg'),
			 d = $.getSize($(document)),
			 yes = c.find('.yes'),
			 no = c.find('.no'),
			 re = null;
		bg.width((d.w)).height((d.h)).css({'opacity':.3,'top':0,'left':0}).fadeIn();
		c.pos();
		$(document).on({'keydown':function(e){
			if(e.keyCode==13) {
				if(opt.yes)
					opt.yes()
				$.delConfirmd();
				return true;
			}else{
				if(opt.no)
					opt.no()
				$.delConfirmd();
				return false;
			}	
		}})
		yes.live('click',function(){
			if(opt.yes)
				opt.yes()
			$.delConfirmd();
			return true;
		})
		no.bind('click',function(){
			if(opt.no)
				opt.no()
			$.delConfirmd();
			return false;
		})
	},
	addConfirmd:function(str){ //添加模态
		var confirmHmtl = '\
		<div class="msgContentClassBg"></div>\
		<div class="LDFconfirm">\
			<p class="cont">'+str+'</p>\
			<p class="bottom">\
				<a href="javascript:;" class="yes">确定</a>\
			 	<a href="javascript:;" class="no">取消</a>\
			</p>\
		</div>\
		';
		$('body').append(confirmHmtl);
	},
	delConfirmd:function(){ //删除模态框
		$('.msgContentClassBg').remove();
		$('.LDFconfirm').remove();
	}
});
//====================================================

$(document).ready(function(){
	var s1={
		time:5000,					//轮播时间单位秒
		numHeight: 15, 			//运动条整体高度
		width: 70, 					//运动条宽度
		height: 10, 				//运动条高度
		actHeight: 12, 			//当前运动条高度
		position: 'bottom:12px', //运动条位置
		titlePos: 'bottom:35px',		//标题位置
		border: 1,					//运动条边框粗细
		borderColor: '#fff', 	//运动条边框颜色
		bgColor: '#eee', 			//运动条背景颜色
		extendOutCss:'background:#999;margin-right:15px;border-radius:5px;', //运动条外框自定义css
		extendCss:'border-radius:5px;box-shadow:inset 0 0 2px #fff,0 0 5px #fff,0 0 10px #fff;0 0 15px #fff;0 0 20px #fff;0 0 25px #fff;0 0 30px #fff;0 0 45px #fff;0 0 50px #fff;' //运动条扩展css
	}
	new slided('#indexs',s1);

	new roll('#s2');

	//menu
	var m=0,menuli=$('.menu li'),timerOut=null;
	menuli.each(function(i){
		if($(this).attr('act')==1) m=i;
	})
	$('.movebg').css({'left':m*80+'px'});
	menuli.hover(function(){
		clearTimeout(timerOut);
		var w = $(this).index()*80;
		$('.movebg').stop().animate({'left':w+'px'},200);
	},function(){
		var w = m*80;
		timerOut = setTimeout(function(){
			$('.movebg').stop().animate({'left':w+'px'},200);
		},500)
		
	})

	//menu移动
	
	$(document).scroll(function(){
		if($(window).scrollTop()>=30){
			$('.fixMenu').css({'top':$(window).scrollTop()-30+'px','opacity':'.9'});
		}else{
			$('.fixMenu').css({'top':'0px'})
		}
	})
	//頻道圖片
	$('.channel').hover(function(){
		$(this).find('img').stop().animate({'left':'-30px'},300);
	},function(){
		$(this).find('img').stop().animate({'left':'0px'},300);
	})
	//活动全部下拉
	$('.allhuodong').hover(function(){
		$('.huodong').show();
		$('.allhuodongcontent').text('▲');
	},function(){
		$('.huodong').hide();
		$('.allhuodongcontent').text('▼');
	})
	//系列活动划过图片出现文字
	$('.xlist').hover(function(){
		$(this).find('.bga').stop().fadeIn();
	},function(){
		$(this).find('.bga').stop().fadeOut();
	})
	//评论区域
	$('#commcont').keyup(function(){ com(); })
	$('#commcont').mouseenter(function(){ com(); })
	$('#commcont').mouseleave(function(){ com(); })

	
	//ajax评论分页
	var commentData=[];
	$('.pageAjax a').live('click',function (){
		var url = $(this).attr('href');
		if(commentData[url]){
			$('#commentWrap').html(commentData[url]);
		}else{
			$.msg({'timeout':5000,css:{width:150,height:60},str:'<i class="icon-spinner icon-4x icon-spin blue pull-left"></i><span class="black fm">精彩评论正在加载中...</span>'});
			$.post(url,null,function(re){
				if(re) $.msg({timeout:0}).close();
				commentData[url]=re;
				$('#commentWrap').html(commentData[url]);
			})
		}
		return false;
	})


});

function com(){
	var t=$('#commcont').val(),
		 s=2000-t.length;
	if(s<=0) {
		$('#commcont').val(t.slice(0, 2000));
		s=0;
	}
	$('.zishu').text(s)
}

//活动轮播图==============
function roll(id){
	this.id=$(id);
	this.c=0;
	this.timer=null;
	this.getDom().addNum().auto().adde();
}
roll.prototype.getDom=function(){
	this.pic=this.id.children('.pic');
	this.a = this.pic.find('a');
	this.sub = this.id.children('.slideSub');
	this.t = this.sub.children('div');
	return this;
}
roll.prototype.move=function(){
		this.a.eq(this.c).show().siblings('a').hide();
		this.t.eq(this.c).show().siblings().hide();
		this.na.eq(this.c).css('opacity','1').siblings().css('opacity','.3');
	return this;
}
roll.prototype.auto=function(){
	var _this = this;
	this.timer=setInterval(function(){
		_this.c++;
		if(_this.c>=_this.a.length) _this.c=0;
		_this.move();
	},3000)
	return this;
}
roll.prototype.addNum=function(){
	var html="<div class='rollNum' style='position:absolute;bottom:8px;left:15px;line-height:25px;height:25px;width:50%;z-index:5;'>";
		for(var i=0; i<this.a.length;i++){
			html+="<a style='border-radius:50%;border:1px solid #ddd;background:#fff;width:20px;height:20px;display:inline-block;margin-right:5px;'></a>";
		}
		html+="</div>";
	this.id.append(html);
	this.num=this.id.children('.rollNum');
	this.na=this.num.find('a');
	this.na.eq(this.c).css('opacity','1').siblings().css('opacity','.3');
	return this;
}
roll.prototype.adde=function(){
	var _this=this;
	this.na.hover(function(){
		clearInterval(_this.timer);
		_this.c=$(this).index();
		_this.move();
	},function(){
		_this.auto();
	})
	return this;
}




//检测登陆
	
function checkLogin(){ //检测用户是否登录
	$('#Login').remove();
	if(!sessionUid){
		$.msg({
			str:loginHtml.html(),
			shade:1,
			closebtn:1,
			timeout:5000,
			css:{width:600,height:350}
		})
		return false;
	}else{
		return true;
	}
}
//ajax登陆，发送表单数据
function loginAjax(id){
	var url=$(id).attr('action'),formData=$(id).serialize();
	$.post(url,formData,function(re){
		if(re=='1'){
			$.msg({timeout:3,str:'<i class="icon-thumbs-up icon-large green">登陆成功！',css:{width:100,height:20},close:function(){
				location.reload();
			}}).close();
			
		}else{
			alert('用户名或者密码错误...');
		}
	})
	return false;
}